package ldh.im.fxbase.component;

import com.gluonhq.emoji.Emoji;
import com.gluonhq.emoji.EmojiData;
import com.gluonhq.emoji.EmojiLoaderFactory;
import com.gluonhq.emoji.util.TextUtils;
import javafx.animation.ScaleTransition;
import javafx.geometry.Insets;
import javafx.geometry.Side;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.Tooltip;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.stage.Popup;
import javafx.util.Duration;
import ldh.im.fxbase.util.PopupPos;
import ldh.im.fxbase.util.RegionUtil;

import java.util.List;
import java.util.function.Consumer;

public class EmojiPane2 extends TabPane {

    private Popup popup = new Popup();
    private Consumer<String> consumer;

    public EmojiPane2() {
        if (!EmojiLoaderFactory.getEmojiImageLoader().isInitialized()) {
            EmojiLoaderFactory.getEmojiImageLoader().initialize().thenAccept(aBoolean -> {
                if (aBoolean) {
                    init();
                }
            });
        }
    }

    public void show(Region region, Consumer<String> consumer) {
        double x = RegionUtil.calcAnchorX(region, PopupPos.up_east, this);
        double y = RegionUtil.calcAnchorY(region, PopupPos.up_east, this);
        popup.show(region.getScene().getWindow(), x, y);
        this.consumer = consumer;
    }

    private void init() {
        this.getStyleClass().add("emoji-pane");
        this.setTabDragPolicy(TabDragPolicy.FIXED);
        this.setSide(Side.BOTTOM);
        this.setPrefSize(625, 400);
        this.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE);
        Tab tab1 = createTab("表情", "People & Body", ":smiley:");
        Tab tab2 = createTab("自然界", "Animals & Nature", ":dog:");
        Tab tab3 = createTab("食物", "Food & Drink", ":apple:");
        Tab tab4 = createTab("活动", "Activities", ":soccer:");
        Tab tab5 = createTab("出行", "Travel & Places", ":airplane:");
        Tab tab6 = createTab("物品", "Objects", ":bulb:");
        Tab tab7 = createTab("标签", "Symbols", ":atom:");
        this.getTabs().addAll(tab1, tab2, tab3, tab4, tab5, tab6, tab7);

        popup.getContent().add(this);
        popup.setAutoHide(true);
    }

    private Tab createTab(String title, String category, String iconName) {
        Tab tab = new Tab(title);
        tab.setId(title);

        ImageView icon = (ImageView) TextUtils.convertUnifiedToImageNode(iconName);
        icon.setFitWidth(20);
        icon.setFitHeight(20);
        tab.setGraphic(icon);
        tab.setText("");

        FlowPane flowPane = new FlowPane(10, 10);

        flowPane.setPadding(new Insets(10));
        List<Emoji> emojiList = EmojiData.emojiFromCategory(category);
        emojiList.forEach(emoji -> flowPane.getChildren().add(createEmojiNode(emoji)));

        ScrollPane scrollPane = new ScrollPane(flowPane);
        scrollPane.setFitToHeight(true);
        scrollPane.setFitToWidth(true);

        tab.setContent(scrollPane);

        tab.selectedProperty().addListener(ee-> {
            if(tab.getGraphic() == null) return;
            if(tab.isSelected()) {
                tab.setText(tab.getId());
            } else {
                tab.setText("");
            }
        });
        return tab;
    }

    private Node createEmojiNode(Emoji emoji) {
        StackPane stackPane = new StackPane();
        stackPane.setMaxSize(20, 20);
        stackPane.setPrefSize(20, 20);
        stackPane.setMinSize(20, 20);
        stackPane.setPadding(new Insets(3));

        ImageView imageView = (ImageView) TextUtils.convertUnifiedToImageNode(emoji.getUnified());
        imageView.setFitWidth(20);
        imageView.setFitHeight(20);
        stackPane.getChildren().add(imageView);

        Tooltip tooltip = new Tooltip(emoji.getText());
        Tooltip.install(stackPane, tooltip);
        stackPane.setCursor(Cursor.HAND);
        ScaleTransition st = new ScaleTransition(Duration.millis(90), imageView);

        stackPane.setOnMouseEntered(e-> {
            //stackPane.setStyle("-fx-background-color: #a6a6a6; -fx-background-radius: 3;");
            imageView.setEffect(new DropShadow());
            st.setToX(1.2);
            st.setToY(1.2);
            st.playFromStart();
        });
        stackPane.setOnMouseExited(e-> {
            //stackPane.setStyle("");
            imageView.setEffect(null);
            st.setToX(1.);
            st.setToY(1.);
            st.playFromStart();
        });

        stackPane.setOnMouseClicked(e->{
//            String emojiSpan = String.format("<img class='emoji' src='%s'>", ImageCache.getInstance().getFilePath(emoji.getHex()));
//            if (isHex) emojiSpan = "&#x" + emoji.getHex() + ";";
            String emojiSpan = emoji.getCodeName();
            consumer.accept(emojiSpan);

        });
        return stackPane;
    }
}

